import { setGlobal } from '@swda/utils'
//--------------------------------------------------------------------------
// Window_EnemyList
// 敌人列表
// 显示条件：
// 敌人ID <= 330
// 标记为 <EnemyBook Hide> 的敌人
// 标记为 <EnemyBook Duplicate: x> 的重复敌人（x：该敌人记录到x号敌人的神魔录中）
	
function Window_EnemyList() {
	this.initialize.apply(this, arguments);	
};
	
Window_EnemyList.prototype = Object.create(Window_Selectable.prototype);
Window_EnemyList.prototype.constructor = Window_EnemyList;
	
Window_EnemyList.prototype.initialize = function(x, y, w, h) {
	this.hiddenEnemies = [];
    this._category = 'none';

	Window_Selectable.prototype.initialize.call(this, x, y, w, h);
	this.refresh();
};


Window_EnemyList.prototype.setCategory = function(category) {
    if (this._category !== category) {
        this._category = category;
        this.refresh();
        this.resetScroll();
    }
};

Window_EnemyList.prototype.includes = function(item) {
    if (item)
        return (item.id <= 330 && item.classId === (this._category + 6)) && !(item.bookHide) && !(item.duplicatedId !== 0);
    else
        return false;
};

Window_EnemyList.prototype.makeItemList = function() {
    this._data = $dataEnemies.filter(function(item) {
        return this.includes(item);
    }, this);
    if (this.includes(null)) {
        this._data.push(null);
    }

    // 敌人排序
    // boss不按等级排序
    if (this._category == 16)
        return;
    this._data.sort(function (a, b) {
        if (!a || !b)
            return
        if (!a.id || !b.id)
            return
        // 护驾按等级排序
        if (!a.catchLevel || !b.catchLevel)
            return
        return a.catchLevel - b.catchLevel       
    });
};



Window_EnemyList.prototype.drawItem = function(index) {
    var item = this._data[index];
    if (item) {
        let txt = "";
        const rect = this.itemRectForText(index);
        if ($gameSystem.isInEnemyBook(item)){
            txt = item.name.split('（')[0];
            this.drawText(txt, rect.x, rect.y, rect.width);
            const txtIndentify = "["+ (Array(3).join('0') + $gameSystem.identifyRate(item.id)).slice(-3) +"%]";
            this.setSmallerFont();
            this.drawText(txtIndentify, rect.x, rect.y, rect.width, 'right');
        } else {
            txt = "？？？"
            this.drawText(txt, rect.x, rect.y, rect.width);
        }
        this.resetFontSettings();
    }
};
Window_EnemyList.prototype.refresh = function() {
    this.makeItemList();
    this.createContents();
    this.drawAllItems();
};

Window_EnemyList.prototype.maxItems = function() {
    return this._data ? this._data.length : 1;
};


// select
var originSelect = Window_EnemyList.prototype.select
Window_EnemyList.prototype.select = function (index) {
    originSelect.call(this, index)
    //if(index == 0 && !!$gameParty.lastEnemyBookEnemy())
        //if($gameParty.lastEnemyBookEnemy()[1]>0)
            //asdkfljadsklf
    this.$emit('select', ...arguments)

    let lastSelectInfo = [null, null];
    if(index>=0){
        const enemy = this._data[index];
        if(!!enemy)
            lastSelectInfo = [enemy.classId, enemy.id];
        $gameParty.setLastEnemyBookEnemy(lastSelectInfo);
    }

}

// deselect
var originDeSelect = Window_EnemyList.prototype.deselect
Window_EnemyList.prototype.deselect = function () {
    originDeSelect.call(this)
    this.$emit('deselect', ...arguments)
}

setGlobal('Window_EnemyList', Window_EnemyList)